package training.minggu5.maven.ui.servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import training.minggu5.maven.domain.Barang;
import training.minggu5.maven.service.BarangService;


public class HalamanMuka extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        
        ApplicationContext ctx = WebApplicationContextUtils
                .getWebApplicationContext(req.getSession().getServletContext());
        BarangService barvice = (BarangService) ctx.getBean("barangService");
        List<Barang> list = barvice.findAllBarang();

        resp.setContentType("text/html");
        String output = "<html>";
        output+="<head>";
        output+="<title>";
        output+="halaman muka";
        output+="</title>";
        output+="</head>";
        output+="<body>";
        output+="<h1>ini tampilan depannya</h1>";
        output+="<h2>list barang:</h2>";
        output+="<table><tr><th>kode</th><th>nama barang</th><th>unit price</th></tr>";
        for(Barang bar : list){
            output+="<tr><td>"+bar.getKodeBarang()+"</td>";
            output+="<td>"+bar.getNamaBarang()+"</td>";
            output+="<td>"+bar.getUnitPrice()+"</td>";
            output+="<td><a href='editBarang?k="+bar.getKodeBarang()+"'>edit</a> | </td>";
            output+="<td><a href='deleteBarang?k="+bar.getKodeBarang()+"'>delete</a></td></tr>";
        }
        output+="</table><a href='insertBarang'>simpan baru</a></body>";
        output+="</html>";
        resp.getWriter().print(output);
        resp.getWriter().close();
    }

}
